package cn.tedu.hospitalgroupproject.server.content.controller;


import cn.tedu.hospitalgroupproject.server.common.pojo.vo.PageData;
import cn.tedu.hospitalgroupproject.server.common.web.JsonResult;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.mapper.DepartmentMapper;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DepartmentAddNewParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DepartmentUpdateInfoParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DepartmentListVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DepartmentStandardVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorTitleAppLimitListVO;
import cn.tedu.hospitalgroupproject.server.content.service.IDepartmentService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/management/departments")
@Api(tags="1.2 管理部分-科室管理")
@Validated
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    public DepartmentController() {
        log.info("创建控制器对象:DepartmentController");
    }

    /*添加科室*/
    @PostMapping("/add-new")
    @ApiOperation("新增科室")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Validated DepartmentAddNewParam departmentAddNewParam){
        log.debug("下面开始处理{}添加科室请求",departmentAddNewParam);
        departmentService.addNew(departmentAddNewParam);
        return JsonResult.ok();
    }

    @ApiOperation("删除科室")
    @ApiOperationSupport(order = 150)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "科室ID", required = true, dataType = "long")
    })
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable @Range(min = 1, message = "删除医生数据失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【删除指定科室ID的数据】的请求，参数：{}", id);
        departmentService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询科室列表")
    @ApiOperationSupport(order = 220)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    @GetMapping("/list")
    public JsonResult listDoctor(Integer page, String queryType) {
        log.debug("开始处理【查询科室列表】请求，页码：{}", page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<DepartmentListVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = departmentService.list(1, Integer.MAX_VALUE);
        } else {
            pageData = departmentService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }

    @ApiOperation("更新指定ID下的科室数据")
    @ApiOperationSupport(order = 220)
    @PostMapping("/{id:[0-9]+}/update/info")
    public JsonResult updateInfoById(@Validated DepartmentUpdateInfoParam departmentUpdateInfoParam){
        log.debug("开始处理[根据ID更新指定科室]请求,新标签的内容:{}",departmentUpdateInfoParam);
        departmentService.updateInfoById(departmentUpdateInfoParam);
        return JsonResult.ok();
    }


    @ApiOperation("根据科室ID查询数据")
    @ApiOperationSupport(order = 230)
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="科室ID",required = true,dataType = "Long")})
    @GetMapping("/{id:[0-9]+}")
    public JsonResult getStandardById(@PathVariable("id") @Range(min=1,message="获取科室数据失败,请输入合法标签!") Long id){
        log.debug("开始处理[根据ID查找指定科室]请求,科室id:{}",id);
        DepartmentStandardVO departmentStandardVO=departmentService.getStandardById(id);
        return JsonResult.ok(departmentStandardVO);
    }

}
